package com.oa.service.impl;

import com.oa.mapper.DeptMapper;
import com.oa.pojo.Dept;
import com.oa.service.DeptService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 吴世荣
* @description 针对表【dept】的数据库操作Service实现
* @createDate 2023-11-02
*/
@Service
public class DeptServiceImpl implements DeptService{
    @Autowired
    private DeptMapper deptMapper;
    @Override
    public List<Dept> selectDept(Dept dept) {
        int flag=0;
        List<Dept>deptList =deptMapper.selectAll(dept);
        return deptList;
    }
    @Override
    public int deleteDept(Integer id) {
        Dept dept = new Dept();
        dept.setId(id);
        int value=deptMapper.deleteDept(dept);
        /*如果删除成功  则把该状态设置为停用  2  表示停用*/

        if(value==1){
            dept.setStatus(2);
            updateDept(dept);
        }
        return value;
    }
    @Override
    public int insertDept(Dept dept) {
//      新添加的父级部门ID要在以有的上级id中存在  根据级找出所有的上级ID
        Dept dept1 = new Dept();
//      dept.getGrade()-1  得到上一级id的等级  查出全部等级为dept.getGrade()-1的部门
        dept1.setGrade(dept.getGrade()-1);
        List<Dept> deptList=deptMapper.selectAll(dept1);
//      判断当前插入的父级id是否在deptList中
        boolean flag1=false;
        for (Dept d:deptList){
            if (d.getId()==dept.getId()){
//                则存在接着执行
                flag1=true;
                break;
            }
        }
        /*如果不存在  */
        if (!flag1){
            return -1;
        }
        return deptMapper.insertDept(dept);
    }
    @Override
    public int updateDept(Dept dept) {
        return deptMapper.updateDept(dept);
    }
}




